package com.avic.rong.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.bean.HomeBean;
import com.avic.rong.utils.DensityUtils;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.util.ArrayList;
import java.util.List;

/**
 * 关注
 * Creater: Created by wangyz on 2017/1/10.
 * Email: 936804097@qq.com
 */

public class FollowAdapter extends RecyclerView.Adapter<FollowAdapter.FollowViewHolder> {

    private static final int IS_HEADER = 2;
    private static final int IS_NORMAL = 3;
    private Context mContext;
    private List<HomeBean.CriterionBean> data = new ArrayList<>();
    private PublicNumberAdapter publicNumberAdapter;

    public FollowAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<HomeBean.CriterionBean> list){
        this.data = list;
        notifyDataSetChanged();
    }

    @Override
    public FollowAdapter.FollowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.view_follow, parent, false);
        if (viewType == IS_HEADER){
            view = LayoutInflater.from(mContext).inflate(R.layout.view_follow_header, parent, false);
        }
        FollowViewHolder holder = new FollowViewHolder(view, viewType);

        return holder;
    }

    @Override
    public void onBindViewHolder(FollowAdapter.FollowViewHolder holder, final int position) {

        if (holder.viewType == IS_HEADER){


            publicNumberAdapter = new PublicNumberAdapter(mContext);
            List<HomeBean.CriterionBean> list = new ArrayList<>();
            HomeBean.CriterionBean bean = new HomeBean.CriterionBean();
            bean.setTargetName("dasdfasd");
            list.add(bean);
            HomeBean.CriterionBean bean2 = new HomeBean.CriterionBean();
            bean2.setTargetName("dasdfasd");
            list.add(bean2);
            HomeBean.CriterionBean bean3 = new HomeBean.CriterionBean();
            bean3.setTargetName("dasdfasd");
            list.add(bean3);
            HomeBean.CriterionBean bean4 = new HomeBean.CriterionBean();
            bean4.setTargetName("dasdfasd");
            list.add(bean4);
            HomeBean.CriterionBean bean5 = new HomeBean.CriterionBean();
            bean5.setTargetName("dasdfasd");
            list.add(bean5);
            HomeBean.CriterionBean bean6 = new HomeBean.CriterionBean();
            bean6.setTargetName("dasdfasd");
            list.add(bean6);
            HomeBean.CriterionBean bean7 = new HomeBean.CriterionBean();
            bean7.setTargetName("dasdfasd");
            list.add(bean7);
            HomeBean.CriterionBean bean8 = new HomeBean.CriterionBean();
            bean8.setTargetName("dasdfasd");
            list.add(bean8);

          //  publicNumberAdapter.setData(list);

            LinearLayoutManager libraryLayoutManager = new LinearLayoutManager(mContext);
            libraryLayoutManager.setOrientation(OrientationHelper.HORIZONTAL);
//
            holder.recycler_public_number.addItemDecoration(new SpaceItemHorizontalDecoration(0, DensityUtils.dp2px(mContext,10)));
            holder.recycler_public_number.setLayoutManager(libraryLayoutManager);
            holder.recycler_public_number.setAdapter(publicNumberAdapter);


        }else if(holder.viewType == IS_NORMAL){

//            GlideApp.with(mContext).load(AppUtils.IMG2_URL + data.get(position).getTargetImg())
//                    .addListener(new EmptyBitmapListener(holder,data.get(position)))
//                    .placeholder(R.mipmap.empty)
//                    .into(holder.img_category);

            holder.tv_category_name.setText(data.get(position).getTargetName());

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mContext.startActivity(DetailProductActivity.getIntent(mContext,"12"));
                }
            });



        }


//        holder.tv_category_price.setText("¥" + data.get(position).getPrice());
//        holder.itemView.setOnClickListener(new CriterionItemOnClickListener(mContext,AppUtils.BASE_URL + "app/good/info/" + data.get(position).getSkuId() + ".html"));

    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return IS_HEADER;
        } else {
            return IS_NORMAL;
        }
    }



    @Override
    public int getItemCount() {
        return data.size();
    }



    class EmptyBitmapListener implements RequestListener<Drawable> {
        private FollowViewHolder holder;
        private HomeBean.CriterionBean recommand;

        public EmptyBitmapListener(FollowViewHolder holder,HomeBean.CriterionBean recommand) {
            this.holder = holder;
            this.recommand = recommand;
        }

        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {

            if (!TextUtils.isEmpty(recommand.getTargetCatName())){
                holder.tv_content.setVisibility(View.VISIBLE);
                holder.tv_content.setText(recommand.getTargetCatName());
            }
            return false;
        }

        @Override
        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
            return false;
        }
    }


    class FollowViewHolder extends RecyclerView.ViewHolder {

        TextView tv_category_name;
        ImageView img_category;
        TextView tv_content;//无图提示

        RecyclerView recycler_public_number;
        int viewType = IS_HEADER;

        public FollowViewHolder(View itemView, int type) {
            super(itemView);
            viewType = type;
            if (type == IS_HEADER){
                recycler_public_number = itemView.findViewById(R.id.recycler_public_number);
            }else if(type == IS_NORMAL){
                tv_category_name = itemView.findViewById(R.id.tv_category_name);

            }

        }
    }


}
